package com.qfedu.service.impl;

import com.github.wxpay.sdk.WXPay;
import com.qfedu.config.MyWxPayConfig;
import com.qfedu.entity.Orders;
import com.qfedu.mapper.OrderMapper;
import com.qfedu.service.OrderCustomer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者：刘随阳
 * 时间: 2021/9/24 10:08
 * 描述：神光照耀,太白成瑞。六丙来迎,百福悠归
 */
@RabbitListener(queues = "queue_live")
@Service
public class OrderCustomerImpl implements OrderCustomer {

    @Autowired
    private OrderMapper orderMapper;

    private WXPay wxPay = new WXPay(new MyWxPayConfig());

    @Override
    @RabbitHandler
    public void selectOrder(String orderId) throws Exception {

        HashMap<String, String> data = new HashMap<>();
        Orders order = orderMapper.selectByPrimaryKey(orderId);
        if(order.getOrderStatus() == 1){
            data.put("out_trade_no", orderId);
            Map<String, String> respMap = wxPay.orderQuery(data);
            if("SUCCESS".equalsIgnoreCase( respMap.get("trade_state")) ){
                //订单已经支付
                order.setOrderStatus(2);
                System.out.println(orderId+":订单已经支付  ");
            }else {
                //订单未支付
                order.setOrderStatus(6);
                order.setOrderCloseType(1);
                int i = orderMapper.updateByPrimaryKey(order);
                System.out.println(orderId+":订单未支付");
                Map<String, String> closeMap = wxPay.closeOrder(data);
                System.out.println(orderId+"订单已取消  "+closeMap);
            }
        }

    }
}
